package com.edu.robot.modules.robot.corvert;

import cn.hutool.json.JSONUtil;
import com.edu.robot.modules.robot.controller.vo.ToolVO;
import com.edu.robot.modules.robot.dto.ToolConfigDTO;
import com.edu.robot.modules.robot.dto.ToolInfoDTO;
import com.edu.robot.modules.robot.model.CrTool;

import java.util.Optional;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 14:32
 **/


public class ToolCovert {

	public static CrTool convert(ToolVO toolVO) {
		CrTool crTool = new CrTool();

		Optional.ofNullable(toolVO.getId()).ifPresent(id -> {
			crTool.setId(Long.valueOf(id));
		});

		crTool.setName(toolVO.getName());
		crTool.setDescription(toolVO.getDescription());

		ToolConfigDTO toolConfigDTO = new ToolConfigDTO();

		toolConfigDTO.setTrigger(toolVO.getTrigger());
		toolConfigDTO.setUrl(toolVO.getUrl());
		toolConfigDTO.setParameters(toolVO.getParameters());
		toolConfigDTO.setHeaders(toolVO.getHeaders());
		toolConfigDTO.setMethod(toolVO.getMethod());
		toolConfigDTO.setResponse(toolVO.getResponse());
		crTool.setInterfaceType("http");
		crTool.setConfigTemplate(JSONUtil.toJsonStr(toolConfigDTO));
		return crTool;
	}

	public static ToolInfoDTO covert(CrTool crTool){
		ToolInfoDTO toolInfoDTO = new ToolInfoDTO();
		toolInfoDTO.setName(crTool.getName());
		toolInfoDTO.setDescription(crTool.getDescription());
		toolInfoDTO.setToolConfigDTO(JSONUtil.toBean(crTool.getConfigTemplate(), ToolConfigDTO.class));
		toolInfoDTO.setStatus(crTool.getStatus());
		toolInfoDTO.setId(String.valueOf(crTool.getId()));
		return toolInfoDTO;
	}


}
